﻿using System;
using System.Collections.Generic;
using System.Data;

namespace SPDbFileExtractor.Framework.Entities
{
    /// <summary>
    /// Web
    /// </summary>
    public class Web
    {
        /// <summary>
        /// Gets or sets the web id.
        /// </summary>
        /// <value>
        /// The web id.
        /// </value>
        public Guid WebId { get; set; }

        /// <summary>
        /// Gets or sets the site id.
        /// </summary>
        /// <value>
        /// The site id.
        /// </value>
        public Guid SiteId { get; set; }

        /// <summary>
        /// Gets or sets the full URL.
        /// </summary>
        /// <value>
        /// The full URL.
        /// </value>
        public string FullUrl { get; set; }

        /// <summary>
        /// Gets or sets the parent web id.
        /// </summary>
        /// <value>
        /// The parent web id.
        /// </value>
        public Guid? ParentWebId { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the created.
        /// </summary>
        /// <value>
        /// The created.
        /// </value>
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets or sets the created by.
        /// </summary>
        /// <value>
        /// The created by.
        /// </value>
        public string CreatedBy { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return (Title == null || Title.Trim().Length == 0) ? WebId.ToString() : Title.Trim();
        }

        /// <summary>
        /// Gets the collection from table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static List<Web> GetCollectionFromTable(DataTable table)
        {
            if (table == null || table.Rows.Count == 0)
            {
                return new List<Web>();
            }

            var result = new List<Web>();

            foreach (DataRow row in table.Rows)
            {
                Web web = new Web();
                web.Created = (DateTime)row["Created"];
                web.CreatedBy = row["CreatedBy"] as string;
                web.FullUrl = row["FullUrl"] as string;
                web.ParentWebId = row["ParentWebId"] == DBNull.Value ? null : (Guid?)(new Guid(row["ParentWebId"].ToString()));
                web.SiteId = new Guid(row["SiteId"].ToString());
                web.Title = row["Title"] as string;
                web.WebId = new Guid(row["WebId"].ToString());

                result.Add(web);
            }

            return result;
        }
    }
}
